﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using System.Text;
using System.IO;
using System.Dynamic;
using FluorineFx.Json;

namespace lxwmvc
{
    public static class examplecontroller
    {
        /// <summary>
        /// 全局引用
        /// </summary>
        /// <param name="pb"></param>
        public static void _init(pagebase pb)
        {

        }

        [temp("web/page/")]
        //[temp("wap/page/index2")] 这句话查找目录下对应的模板 index2.htm
        public static void index(pagebase pb)
        {
            pb["jsver"] = System.Reflection.Assembly.GetExecutingAssembly().GetName().Version.tostr();
        }

        [temp("wap/page/index")]
        public static void index2(pagebase pb)
        {
#if DEBUG
            //默认已经登录信息
            //pb.Session["g_user_id"] = "";
#else
#endif

            pb["jsver"] = System.Reflection.Assembly.GetExecutingAssembly().GetName().Version.tostr();
        }

        /// <summary>
        /// 获取用户信息,并返回json
        /// </summary>
        /// <param name="pb"></param>
        [json]
        public static void yonghudata(pagebase pb)
        {
            //获取json对象
            //JavaScriptObject json = pb.bodyjson();
            //把json转换成对象，只转换第一层对象
            //supermodel sm = supermodel.create()
            BaseJson bj = pb.JSON<BaseJson>();
            //bj.data = 
        }

        [json]
        public static void getallusersinfo(pagebase pb)
        {
            //获取json对象
            //JavaScriptObject json = pb.bodyjson();
            //把json转换成对象，只转换第一层对象
            //supermodel sm = supermodel.create()
            //dynamic user = supermodel.create();

            BaseJson bj = pb.JSON<BaseJson>();
            bj.data = supermodel.create("yonghu").list().tojson();
        }

       

        /// <summary>
        /// 数据操作测试列子1
        /// </summary>
        /// <param name="pb"></param>
        public static void testsupermodel(pagebase pb)
        {
            {
                //_address_ addr = new _address_();

                //_yonghu yonghu = new _yonghu();
                //yonghu.zhuceriqi = DateTime.Now;
                //yonghu.saveorupdate();

                _ziyuan drhk = queryhelper.get<_ziyuan>("select top 1 null as kkk,*,1 as tt from ziyuan");


                //var BankInfo = queryhelper.query(" select SysUser_Bankid,BankTypeid,SysUser_BankType.BankType,(select SUBSTRING(BankCardNum,13,16)) as BankCardNum  FROM SysUser_Bank INNER JOIN SysUser_BankType  ON SysUser_BankType.SysUser_BankTypeid=SysUser_Bank.BankTypeid where 1=1 order by UseDefault");

                ////var BankInfo2 = queryhelper.query<_SysUser_BankType>(" select SysUser_Bankid,BankTypeid,SysUser_BankType.BankType,(select SUBSTRING(BankCardNum,13,16)) as BankCardNum  FROM SysUser_Bank INNER JOIN SysUser_BankType  ON SysUser_BankType.SysUser_BankTypeid=SysUser_Bank.BankTypeid where 1=1 order by UseDefault");

                //var BankInfo1 = queryhelper.query<supermodel>(" select SysUser_Bankid,BankTypeid,SysUser_BankType.BankType,(select SUBSTRING(BankCardNum,13,16)) as BankCardNum  FROM SysUser_Bank INNER JOIN SysUser_BankType  ON SysUser_BankType.SysUser_BankTypeid=SysUser_Bank.BankTypeid where 1=1 order by UseDefault");




                var t = drhk;

            }
            {//save
                dynamic yonghu = supermodel.create();
                //金额
                yonghu.jine = "20.02";
                //描述
                yonghu.miaoshu = "测试";
                yonghu.nianling = "20";
                yonghu.xingbie = "男";
                yonghu.xingming = "张三";
                yonghu.zhuceriqi = DateTime.Now;

                yonghu.table("yonghu").saveorupdate();
            }

            {//save
                dynamic yonghu = supermodel.create("yonghu");
                //金额
                yonghu.jine = "20.02";
                //描述
                yonghu.miaoshu = "测试";
                yonghu.nianling = "20";
                yonghu.xingbie = "男";
                yonghu.xingming = "张三";
                yonghu.qita1 = 1.0;
                yonghu.zhuceriqi = DateTime.Now;
                yonghu.saveorupdate();
            }

            {//update
                dynamic yonghu = supermodel.create("yonghu");
                //金额
                yonghu.jine = "20.02";
                //描述
                yonghu.miaoshu = "更新" + DateTime.Now;
                yonghu.nianling = "20";
                yonghu.xingbie = "男";
                yonghu.xingming = "张三";
                yonghu.zhuceriqi = DateTime.Now;
                yonghu.yonghuid = 2;
                yonghu.saveorupdate();
            }

            {//update
                dynamic yonghu = supermodel.create("yonghu");
                //金额
                yonghu.jine = "20.02";
                //描述
                yonghu.miaoshu = "更新" + DateTime.Now;
                yonghu.nianling = "20";
                yonghu.xingbie = null;//数据库里面会更新成NULL
                yonghu.xingming = "张三";
                yonghu.zhuceriqi = null;
                yonghu.yonghuid = 2;
                yonghu.saveorupdate();

                //dynamic yonghu = new lxwdata("yonghu");
                ////其他字段
                //yonghu.qita1 = "";
                ////注册日期
                //yonghu.zhuceriqi = "";

            }

            {
                dynamic yonghu = pb.model("yonghu");
                ////注册日期
                yonghu.zhuceriqi = DateTime.Now;

                yonghu.saveorupdate();
            }


            {
                var test1 = supermodel.create("yonghu").get();
                var test2 = supermodel.create("yonghu").where("yonghuid=2").get();
                var test3 = supermodel.create("yonghu").select("xingming,xingbie").where("yonghuid=2").get();

                //更新 这句话没有条件，会出现错误。
                //var test4 = supermodel.create().table("yonghu").set("xingming", "lxw").set("xingbie", "2").update();

                var test5 = supermodel
                    .create()
                    .table("yonghu")
                    .set("xingming", "lxw")
                    .set("xingbie", "2")
                    .where("xingming='张三'")
                    .update();

                var test6 = supermodel
                    .create()
                    .table("yonghu")
                    .set("xingming", "lxw3")
                    .set("xingbie", "23")
                    .set("yonghuid", "3")
                    .update(true);
            }

            {//update
                dynamic m = supermodel.create("yonghu").where("yonghuid=2").get();


                //其中一种更新形式，只更新姓名
                m.set("xingming", "lixiaowei");
                m.update(true);

                //另外一种更新形式，所有的键值都更新
                m.xingming = "李晓伟";
                m.saveorupdate();
            }

            {//delete
                supermodel.create("yonghu").where("yonghuid=2").delete();

                //下面不建议这样用
                dynamic m = supermodel.create("yonghu").where("yonghuid=2").get();
                m.delete();

                //可以
                m = supermodel.create("yonghu").where("yonghuid=2");
                m.delete();

                //
            }

            {
                //select 多表
                //var list = supermodel.create("yonghu").inner("zidian", "yonghu.yonghuid=zidian.zidianid").list();
                //var list1 = supermodel.create("yonghu").left("zidian", "yonghu.yonghuid=zidian.zidianid").list();
                //var list2 = supermodel.create("yonghu").select("xingming,count(1)").group("xingming").list();
            }

            {
                //选择 sql语句操作
                //var ret1 = queryhelper.list("select * from yonghu");
                var ret2 = queryhelper.get<supermodel>("select * from yonghu");
                var ret3 = queryhelper.get("select * from yonghu");
                //var ret4 = queryhelper.row("select * from yonghu");
                //var ret5 = queryhelper.rows("select * from yonghu");

                //执行存储过程
                //var pret1 = sqlhelper.pdataset("select * from yonghu");
                //var pret2 = sqlhelper.pobject("select * from yonghu");

                //执行sql语句，出supermodel里面有执行sql语句方法
                var exe1 = queryhelper.execute("update yonghu set xingming='1' where 1=2");

                //执行外部数据库sql语句
                var out1 = sqlhelper.out_exec("select * from yonghu");
                var out2 = sqlhelper.out_getds("select * from yonghu");

            }

            {
                //model操作功能

                yonghu yonghu_ = pb.model().toModel<yonghu>();

            }

            {
                //分页操作

            }

            pb.write("ok");
        }


        /// <summary>
        /// 数据操作测试列子
        /// </summary>
        /// <param name="pb"></param>
        public static void testsupermodel2(pagebase pb)
        {
        }

        /// <summary>
        /// 得到前台数据
        /// </summary>
        /// <param name="pb"></param>
        public static void testsupermodel1(pagebase pb)
        {
            var date = pb.v_date("xxx");
            var @int = pb.v_i("xxx");
            var @string = pb.v_s("xxx");
            var @decimal = pb.v_dec("xxx");
            var @ss = pb.v_ss("xxx");

            var yonghu = pb.model("yonghu");

        }
        [json]
        public static void yonghuinfo(pagebase pb)
        {
            BaseJson json = pb.JSON<BaseJson>();

            json.data = supermodel
                .create("yonghu")
                .select("*")
                .where("yonghuid="+pb.v_i("yonghuid"))
                .get()
                .tojson();
        }

        [json]
        public static void yonghulist(pagebase pb)
        {
            BaseJson json = pb.JSON<BaseJson>();


            //var pi = queryhelper.pager("yonghu", "*", "yonghuid", pageing.pageNo, pageing.pageSize);
            var pi = queryhelper.pager("select * from yonghu", 2, 10);
            //把分页信息输出为json信息
            json.data = pi.tojson();


            dynamic yonghu = supermodel.create("yonghu");
            //金额
            yonghu.jine = "20.02";
            //描述
            yonghu.miaoshu = "更新" + DateTime.Now;
            yonghu.nianling = "20";
            yonghu.xingbie = null;//数据库里面会更新成NULL
            yonghu.xingming = "张三";
            yonghu.zhuceriqi = null;
            //yonghu.yonghuid = 2;
            yonghu.saveorupdate();

            var test6 = supermodel
                    .create()
                    .table("yonghu")
                    .set("xingming", "lxw3")
                    .set("xingbie", "23")
                    .set("yonghuid", "3")
                    .update(true);


            int kk = queryhelper.get<int>("select count(1) from yonghu where yonghuid=@0 and yonghuid=@1", 1,1);

            kk = queryhelper.get<int>("select count(1) from yonghu where 1=@0 ", 1);

            kk = queryhelper.get<int>("select count(1) from yonghu where 1=1 ");

            kk = queryhelper.get<int>("select count(1) from yonghu where 1=@a ", new { a=1});


            json.data = supermodel
                .create("yonghu")
                .select("*")
                .orderby("yonghuid desc")
                .list()
                .tojson();
        }

        [json]
        public static void yonghuinfosave(pagebase pb)
        {
            supermodel sm =  pb.bodyjson().tosm();
            //保存信息
            sm.table("yonghu").saveorupdate();
            BaseJson json = pb.JSON<BaseJson>();
        }

        [json]
        public static void yonghuinfodelete(pagebase pb)
        {
            supermodel
                .create("yonghu")
                .select("*")
                .where("yonghuid=" + pb.v_i("yonghuid"))
                .delete();
        }
    }
}